package com.ys.otms.services.impl;

import com.ys.otms.dao.IYLShipsDao;
import com.ys.otms.dto.ParamDto;
import com.ys.otms.dto.YLShipsDto;
import com.ys.otms.services.IYLShipsServices;
import com.ys.otms.utils.JsonUtil;
import com.ys.otms.utils.SessionUtil;
import com.ys.otms.utils.StringUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/10/12.
 */
@Service
public class YLShipsServiceImpl implements IYLShipsServices {
    private static Logger logger = LoggerFactory.getLogger(YLShipsServiceImpl.class);
    @Autowired
    private IYLShipsDao iylShipsDao;
    @Override
    public JSONObject insertIYLShipsQuery(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("YLShipsServiceImpl insertIYLShipsQuery start obj{}",obj);
        String shipId = JsonUtil.getJSONObjectString(obj,"shipId");
        String classificationId =JsonUtil.getJSONObjectString(obj,"classificationId");
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isNotEmpty(shipId) && StringUtil.isNotEmpty(classificationId)){
            YLShipsDto ylShipsDto = new YLShipsDto();
            ylShipsDto.setClassificationId(Integer.parseInt(classificationId));
            ylShipsDto.setShipId(Integer.parseInt(shipId));
            iylShipsDao.insertIYLShipsQuery(ylShipsDto);
            jsonObject.put("SUCCESS",true);
            jsonObject.put("MSG","新增成功！");
        }else{
            jsonObject.put("SUCCESS",false);
            jsonObject.put("MSG","分类名称或船舶名称不能为空！");
        }
        return jsonObject;
    }

    @Override
    public JSONObject deleteIYLShips(HttpServletRequest request) throws Exception {
        JSONObject obj =JsonUtil.getJSONObject(request);
        logger.info("YLShipsServiceImpl deleteIYLShips start obj{}",obj);
        String shipId = JsonUtil.getJSONObjectString(obj,"shipId");
        String classificationId =JsonUtil.getJSONObjectString(obj,"classificationId");
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isNotEmpty(shipId) && StringUtil.isNotEmpty(classificationId)){
            YLShipsDto ylShipsDto = new YLShipsDto();
            ylShipsDto.setClassificationId(Integer.parseInt(classificationId));
            ylShipsDto.setShipId(Integer.parseInt(shipId));
            iylShipsDao.deleteIYLShips(ylShipsDto);
            jsonObject.put("SUCCESS",true);
            jsonObject.put("MSG","删除成功！");
        }else{
            jsonObject.put("SUCCESS",false);
            jsonObject.put("MSG","请选中船舶！");
        }
        return jsonObject;
    }
}
